using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class StateFrameDefinition : IElementContainer
    {
        private string state;

        private string name;
        private string offset;
        private bool noBaseFrame;

        public StateFrameDefinition(string state, Element element)
        {
            this.state = state;

            sourceElement = element;

            Element subElement = element.FindFirstElement("name", false);
            if (subElement != null)
            {
                name = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("offset", false);
            if (subElement != null)
            {
                offset = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("nobaseframe", false);
            if (subElement != null)
            {
                noBaseFrame = true;
            }
            
        }

        #region IElementContainer Members

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element(state);

            if (!String.IsNullOrEmpty(name))
            {
                Element element = new Element("name");
                element.Value = name;
                sourceElement.ChildElements.Add(element);
            }

            if (!String.IsNullOrEmpty(offset))
            {
                Element element = new Element("offset");
                element.Value = offset;
                sourceElement.ChildElements.Add(element);
            }

            if (noBaseFrame)
            {
                Element element = new Element("nobaseframe");
                sourceElement.ChildElements.Add(element);
            }

        }

        #endregion
    }
}
